const Koa = require('koa')
const path = require('path')
const bodyparser = require('koa-bodyparser')
const ejs = require('ejs')
const session = require('koa-session-minimal')
const MysqlStore = require('koa-mysql-session')
const config = require('./config/default.js')
const router = require('koa-router')
const views = require('koa-views')
const staticCache = require('koa-static-cache')
const app = new Koa()

//session的存储配置
const sessionMysqlConfig = {
    user :config.database.USERNAME,
    password:config.database.PASSWORD,
    database:config.database.DATABASE,
    host:config.database.HOST,
}

//配置session中间件
app.use(session({
    key:'USER_SID',
    store:new MysqlStore(sessionMysqlConfig)
}))

//缓存
app.use(staticCache(path.join(__dirname,'./public'),{dynamic:true},{
    maxAge: 365 * 24 * 60 * 60
}))
app.use(staticCache(path.join(__dirname, './images'), { dynamic: true }, {
    maxAge: 365 * 24 * 60 * 60
}))
//配置服务端模板渲染引擎中间件
app.use(views(path.join(__dirname,'./views'),{
    extension:'ejs'
}))
app.use(bodyparser({
    formLimit:'1mb'
}))

//路由
//注册
app.use(require('./routers/signup.js').routes())
//登录
app.use(require('./routers/signin.js').routes())
//首页
app.use(require('./routers/posts.js').routes())
//退出登录
app.use(require('./routers/posts.js').routes())

// app.use(async(ctx)=>{
//     ctx.body = "<h1>koa2的进阶</h1>"
// })

// app.listen(3000,()=>{
//     console.log('Server starting at port 3000')
// })

app.listen(config.port)